package com.controller;

import com.entity.FUser;
import com.entity.table.FUserTableDef;
import com.mybatisflex.core.query.QueryWrapper;
import com.service.FUserService;
import com.service.impl.FUserServiceImpl;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class LoginController {
    @Autowired
    FUserServiceImpl fUserService;

    @GetMapping(value = "/login")
    public String login() { return "client/login";}

    @PostMapping("/toLogin")
    public String toLogin(FUser user, HttpSession httpSession, Model model){
        QueryWrapper query = QueryWrapper.create()
                .where(FUserTableDef.F_USER.USER_NAME.eq(user.getUserName()))
                .and(FUserTableDef.F_USER.PWD.eq(user.getPwd()));
        FUser tUser = fUserService.getOne(query);
        if (tUser!=null){
            httpSession.setAttribute("user", tUser);
            model.addAttribute("user", tUser);
            return "client/index";
        }
        else {
            model.addAttribute("msg","账号或密码错误,请重新输入!");
            return "client/login";
        }
    }
}
